#include "widget.h"
#include "ui_widget.h"
#include "QDebug"
#include "QTableWidgetItem"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
    //初始化tableWidget
    //插入三行三列
    ui->tableWidget->insertRow(0);
    ui->tableWidget->insertRow(1);
    ui->tableWidget->insertRow(2);
    ui->tableWidget->insertColumn(0);
    ui->tableWidget->insertColumn(1);
    ui->tableWidget->insertColumn(2);
    //设置第几列的表头
    ui->tableWidget->setHorizontalHeaderItem(0,new QTableWidgetItem("姓名"));
    ui->tableWidget->setHorizontalHeaderItem(1,new QTableWidgetItem("学号"));
    ui->tableWidget->setHorizontalHeaderItem(2,new QTableWidgetItem("性别"));
//    //设置第几行的表头，可以用默认的数字
//    ui->tableWidget->setVerticalHeaderItem(0,new QTableWidgetItem("a"));
//    ui->tableWidget->setVerticalHeaderItem(1,new QTableWidgetItem("b"));
//    ui->tableWidget->setVerticalHeaderItem(2,new QTableWidgetItem("c"));

    //填入值
    ui->tableWidget->setItem(0,0,new QTableWidgetItem("张三"));
    ui->tableWidget->setItem(0,1,new QTableWidgetItem("1001"));
    ui->tableWidget->setItem(0,2,new QTableWidgetItem("男"));

    ui->tableWidget->setItem(1,0,new QTableWidgetItem("李四"));
    ui->tableWidget->setItem(1,1,new QTableWidgetItem("1002"));
    ui->tableWidget->setItem(1,2,new QTableWidgetItem("男"));

    ui->tableWidget->setItem(2,0,new QTableWidgetItem("王五"));
    ui->tableWidget->setItem(2,1,new QTableWidgetItem("1003"));
    ui->tableWidget->setItem(2,2,new QTableWidgetItem("女"));

}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_add_clicked()
{
    QString s=ui->lineEdit->text();
    if(s=="")
    {
        //如果listEdit没有写东西，则按下add按钮也没用
        return;
    }
    else
    {
        ui->listWidget->addItem(s);
        //row是自定义的，从0开始
        ui->lineEdit->clear();
    }

}

void Widget::on_pushButton_erase_clicked()
{
    //因为listWidget的元素位置和数组下标一样，从0开始算，所以我们要先-row在去takeItem
    //takeItem的传参是要删除的元素位置
    int r=ui->listWidget->count();
    ui->listWidget->takeItem(r-1);
    //qDebug()<<row<<endl;
}

void Widget::on_pushButton_clicked()
{
    //由于列的数量无法控制，所以只能盲目插入空行，不能填入内容，只能后续自己输入内容
    int r=ui->tableWidget->rowCount();
    ui->tableWidget->insertRow(r);
}

void Widget::on_pushButton_2_clicked()
{
    //减选中行
    int r=ui->tableWidget->currentRow();
    //删除的时候，要找下标位置
    //count返回的是行的个数，要-1

    //int r=ui->tableWidget->rowCount()-1;
    if(r<0)
    {
        return;
    }
    ui->tableWidget->removeRow(r);
}



void Widget::on_pushButton_3_clicked()
{
    //要有列名，不然不能插入空列
    if(ui->lineEdit_2->text()=="")
        return;
    QString s=ui->lineEdit_2->text();
    int c=ui->tableWidget->columnCount();
    ui->tableWidget->insertColumn(c);
    ui->tableWidget->setHorizontalHeaderItem(c,new QTableWidgetItem(s));
    //清除LineEdit原先的内容
    ui->lineEdit_2->clear();
}

void Widget::on_pushButton_4_clicked()
{
    int c=ui->tableWidget->currentColumn();
    //int c=ui->tableWidget->columnCount()-1;
    if(c<0)
        return;
    ui->tableWidget->removeColumn(c);
}
